import React from 'react';
import moment from 'moment';
import router from 'umi/router';
//import Link from 'umi/link'

import { connect } from 'dva';

import {  Button,Form, Input, Select,DatePicker } from 'antd';

const FormItem = Form.Item;

const { Option } = Select;


const linePath = '/AccountStock/Stock';


@connect(({
  accountStock //,accountJournal,resPartner,accountAccount
}) => ({
  accountStock //,accountJournal,resPartner,accountAccount
}))
@Form.create()
class FormCreate extends React.Component {
  state = {
  }

  id = 0

  async componentDidMount() {
    const { location: {query: { id } } } = this.props;

    const { dispatch } = this.props;

    if (id) {
      this.id = Number(id)
      await dispatch({
        type: 'accountStock/view',
        payload:  { id:  this.id }
      });
    }

  }


  handleSubmit = e => {
    const { dispatch, form } = this.props;
    e.preventDefault();


    const create_or_write = async (vals) => {
      if(this.id){
        await dispatch({
          type: 'accountStock/write',
          payload : {
            id: this.id,  vals,
          },
        });

        router.push(`${linePath}?id=${this.id}`);
      }
      else{
        await dispatch({
          type: 'accountStock/create',
          payload : {
            vals,
          },
        });

        const { accountStock } = this.props;
        if (accountStock.id){
          router.push(`${linePath}?id=${accountStock.id}`);
        }
      }
    }


    form.validateFieldsAndScroll( async (err, values) => {
      if (!err) {
        const { date, journal_id, account_id } = values;

        const vals = {
          ...values,
          date: date.format('YYYY-MM-DD'),
        };

        if( ! journal_id ){
          //delete vals.journal_id;
        }

        if( ! account_id ){
          delete vals.account_id;
        }



        console.log( vals );

        await create_or_write(vals)

      }
    })
  }


  render() {
    const { form: { getFieldDecorator }  } = this.props;
    const { accountStock: { record } } = this.props

    return (
      <div>
          <Form onSubmit={this.handleSubmit} >
            <FormItem style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" >提交
              </Button>
            </FormItem>

            <FormItem label="名称">
              {getFieldDecorator('name', {
                rules: [{ required: true }],
                initialValue: record.name,
              })(
                <Input />
              )}
            </FormItem>

            <FormItem label="ref">
              {getFieldDecorator('ref', {
                //rules: [{ required: true }],
                initialValue: record.ref,
              })(
                <Input />
              )}
            </FormItem>

            <FormItem label="日期">
              {getFieldDecorator('date', {
                rules: [{ required: true }],
                initialValue: record.date ? moment(record.date, 'YYYY-MM-DD') : moment()
              })(<DatePicker />)}
            </FormItem>

            <FormItem label="产品类型">
              {getFieldDecorator('product_type', {
                rules: [{ required: true }],
                initialValue: record.product_type
              })(
                <Select
                  placeholder="" >
                  <Option value={'material'} >原材料</Option>
                  <Option value={'goods'} >库存商品</Option>
                </Select>

              )}
            </FormItem>

            <FormItem label="出入库方向">
              {getFieldDecorator('direction', {
                rules: [{ required: true }],
                initialValue: record.direction
              })(
                <Select
                  placeholder="" >
                  <Option value={'in'} >入库</Option>
                  <Option value={'out'} >出库</Option>
                </Select>

              )}
            </FormItem>

            <FormItem label="出入库方式">
              {getFieldDecorator('move_type', {
                rules: [{ required: true }],
                initialValue: record.move_type
              })(
                <Select
                  placeholder="" >
                  <Option value={'open'} >期初</Option>
                  <Option value={'inventory'} >盘点</Option>
                  <Option value={'production'} >生产</Option>
                  <Option value={'purchase'} >采购</Option>
                  <Option value={'sale'} >销售</Option>
                </Select>

              )}
            </FormItem>


          </Form>


      </div>

    );
  }
}


export default FormCreate


